package com.xgxx.chendawei.common.util;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.joda.time.DateTime;
import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter;

import java.time.Duration;
import java.time.Instant;
import java.util.Date;
import java.util.regex.Pattern;

@Slf4j
public class DateUtil {

    // 默认时间格式
    public static final String DEFAULT_DATE_TIME_FORMAT = "yyyy-MM-dd HH:mm:ss";

    //默认日期格式
    public static final String DEFAULT_DATE_FORMAT = "yyyy-MM-dd";


    /**
     * 时间转换<br>
     * 将时间格式转换成字符串，yyyy-MM-dd
     *
     * @param date Date
     * @return String
     */
    public static String formatDate(Date date) {
        if (null == date) {
            return "";
        }
        return DateFormatUtils.format(date, DEFAULT_DATE_FORMAT);
    }

    /**
     * 时间转换<br>
     * 将时间格式转换成字符串，yyyy-MM-dd HH:mm:ss
     *
     * @param date Date
     * @return String
     */
    public static String formatDateTime(Date date) {
        if (null == date) {
            return "";
        }
        return DateFormatUtils.format(date, DEFAULT_DATE_TIME_FORMAT);
    }

    /**
     * 解析，默认格式 yyyy-MM-dd HH:mm:ss
     *
     * @param date 字符串
     * @return 日期
     */
    public static Date parse(String date) {
        DateTimeFormatter format = DateTimeFormat.forPattern(DEFAULT_DATE_TIME_FORMAT);
        return DateTime.parse(date, format).toDate();
    }


    public static Date getYesterdayBegin() {
        return new DateTime(DateTime.now().toString(DEFAULT_DATE_FORMAT)).minusDays(1).toDate();
    }

    public static Date getTodayBegin() {
        return new DateTime(DateTime.now().toString(DEFAULT_DATE_FORMAT)).toDate();
    }

    public static Date getDateNDaysBefore(Integer inDays) {
        if (null == inDays) {
            inDays = 0;
        }
        return new DateTime(DateTime.now().toString(DEFAULT_DATE_FORMAT)).minusDays(inDays).toDate();
    }

    public static Date getMonthMinus(int i) {
        return new DateTime(DateTime.now().toString(DEFAULT_DATE_FORMAT)).minusMonths(i).toDate();
    }

    /**
     * 获取两个日期时间差(分钟)
     *
     * @param beginDate
     * @param endDate
     * @return
     */
    public static long getTimePeriodMinutes(Date beginDate, Date endDate) {
        Instant begin = beginDate.toInstant();
        Instant end = endDate.toInstant();
        Duration duration = Duration.between(begin, end);
        return duration.toMinutes();
    }

    /**
     * 获取两个日期时间差(小时)
     *
     * @param beginDate
     * @param endDate
     * @return
     */
    public static long getTimePeriodHours(Date beginDate, Date endDate) {
        Instant begin = beginDate.toInstant();
        Instant end = endDate.toInstant();
        Duration duration = Duration.between(begin, end);
        return duration.toHours();
    }

    public static Date tryParse(String date) {
        if (StringUtils.isBlank(date)) {
            return null;
        }
        if (date.contains("/")) {
            date = date.trim().replaceAll("/", "-");
        }
        try {
            DateTimeFormatter format = DateTimeFormat.forPattern(DEFAULT_DATE_TIME_FORMAT);
            if (date.length() == DEFAULT_DATE_TIME_FORMAT.length()) {
                return DateTime.parse(date, format).toDate();
            }
            if (date.length() == DEFAULT_DATE_FORMAT.length()) {
                format = DateTimeFormat.forPattern(DEFAULT_DATE_FORMAT);
                return DateTime.parse(date, format).toDate();
            }
            String datePattern = "yyyy-MM-dd HH:mm:ss SSS";
            if (date.length() == datePattern.length()) {
                format = DateTimeFormat.forPattern(datePattern);
                return DateTime.parse(date, format).toDate();
            }
            if (Pattern.matches("^\\d{4}-\\d{2}-\\d{2}\\s\\d{2}:\\d{2}$", date)) {
                datePattern = "yyyy-MM-dd HH:mm";
                format = DateTimeFormat.forPattern(datePattern);
                return DateTime.parse(date, format).toDate();
            }
            if (Pattern.matches("^\\d{4}-\\d{2}-\\d{2}\\s\\d{2}$", date)) {
                datePattern = "yyyy-MM-dd HH";
                format = DateTimeFormat.forPattern(datePattern);
                return DateTime.parse(date, format).toDate();
            }
            return DateTime.parse(date, format).toDate();
        } catch (Exception e) {
            log.warn("tryParse fail .date:{}", date, e);
            return null;
        }
    }
}
